package top.sehnsucht.dao.impl;

import org.apache.log4j.Logger;
import top.sehnsucht.dao.PhotoDao;
import top.sehnsucht.util.DBUtil;
import top.sehnsucht.vo.Photo;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: Cai
 * @CreateTime: 2021/11/5
 */
@Deprecated
public class PhotoDaoImpl implements PhotoDao {

    private Logger logger=Logger.getLogger(this.getClass());

    @Override
    public ArrayList<Map<String, String>> getAllPhotos() {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        ArrayList<Map<String, String>> arrs = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            String sql = "select id,name,photourl from photo order by id";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                Map<String, String> map = new HashMap();
                Integer id = rs.getInt(1);
                String name = rs.getString(2);
                String photourl = rs.getString(3);
                logger.debug("查询: " + id + " " + name + " " + photourl);
                map.put("id", id.toString());
                map.put("name", name);
//                map.put("photourl",photourl);
                arrs.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return arrs;
    }

    @Override
    public Photo getPhotoById(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        Integer thisId = null;
        String name = null;
        String photourl = null;
        try {
            conn = DBUtil.getConn();
            System.out.println("id:" + id);
            String sql = "select * from photo where id = " + id;
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                thisId = rs.getInt(1);
                name = rs.getString(2);
                photourl = rs.getString(3);
                logger.debug(thisId);
                logger.debug(name);
                logger.debug(photourl);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Photo(thisId, name, photourl);
    }

    @Override
    public Integer photoCount() {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        int ans = 0;
        try {
            conn = DBUtil.getConn();
            String sql = "select id,name from photo ";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                ans++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return ans;
    }

    @Override
    public void addPhoto(String name, String photourl) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConn();
            String sql = "insert into photo (name,photourl)values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, photourl);
            int count = ps.executeUpdate();
            if (count > 0) {
                logger.debug("插入成功！");
            } else {
                logger.error("插入失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public Integer getMaxId() {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        int ans = 0;
        try {
            conn = DBUtil.getConn();
            String sql = "select max(id) from photo";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                ans = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return ans;
    }
}
